package msigraphx.gibbsSampler;

import java.util.List;
import java.util.Random;

import com.rits.cloning.Cloner;

public class RandomVariableValue<Type> implements Cloneable {
	public Type val;
	private RandomVariable<Type> randVar;
	
	private RandomVariableValue() {}
	
	public RandomVariableValue(RandomVariable<Type> randVar) {
		this.randVar = randVar;
		List<RandomVariableValue<Type>> allowedValues = randVar.getAllowedValues();
		if(allowedValues != null && allowedValues.size() > 0) {
			Random rand = new Random();
			val = allowedValues.get(rand.nextInt(allowedValues.size())).val;
		}
	}
	
	public RandomVariableValue(RandomVariable<Type> randVar, Type val) {
		this.randVar = randVar;
		this.val = val;
	}
	
	/*public Object clone() {
		RandomVariableValue<Type> newInst = new RandomVariableValue<Type>();
		Cloner cloner = new Cloner();
		newInst.val = cloner.deepClone(val);
		newInst.randVar = this.randVar; //not cloneing because this.randVar is only a callback reference and will never be used for primary storage of RandomVariable
		return newInst;
	}*/
	
	public boolean equals(Object other) {
		if(!(other instanceof RandomVariableValue<?>)) {
			return false;
		}
		RandomVariableValue<?> otherRandVarVal = (RandomVariableValue<?>)other;
		if(!otherRandVarVal.getClass().equals(this.getClass())) {
			return false;
		}
		if(!otherRandVarVal.val.equals(this.val)) {
			return false;
		}
		if(!otherRandVarVal.randVar.equals(this.randVar)) {
			return false;
		}
		
		return true;
	}
	
	public int hashCode() {
		return val.hashCode();
	}

	public void setRandVar(RandomVariable<Type> randVar) {
		this.randVar = randVar;
	}

	public RandomVariable<Type> getRandVar() {
		return randVar;
	}
	
	public String toString() {
		return val.toString();
	}
}
